import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/parameters.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:doctor_prescribe_lib/western_medicine/medical_search/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import '../../prescribe_util.dart';
import '../medical_search/entity/medical_search_entity.dart' as DrugSearchBean;

///药品搜索
class MedicalSearchPage extends BaseBlocPage<MedicalSearchBloc, _State> {
  MedicalSearchPage(Parameters parameters) : super(parameters);

  @override
  _State generateThemeState() {
    return _State();
  }
}

class _State extends BaseBlocPageState<MedicalSearchBloc> {
  @override
  void initState() {
    super.initState();
    bloc.doInit(
        context, parameters.getInt("orgTypeID"), parameters.getString("orgID"));
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            Routers.pop(context);
          },
        ),
        centerTitle: true,
        title: Text('药品搜索'),
      ),
      body: Container(
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.all(10),
              child: TextField(
                onSubmitted: (str) {
                  bloc.doSearch(context, searchKey: str);
                },
                textInputAction: TextInputAction.search,
                style: TextStyle(fontSize: 16),
                decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(left: 10),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10)),
                    hintText: '请输入药品名称或药品简码',
                    hintStyle: TextStyle(fontSize: 12)),
              ),
            ),
            Expanded(
              child: StreamBuilder(
                stream: bloc.listSubject,
                builder: (context,
                    AsyncSnapshot<List<DrugSearchBean.Datas>> snapshot) {
                  return snapshot.hasData
                      ? snapshot.data.isEmpty
                          ? Center(
                              child: Text('很抱歉，未搜索到相关药品~'),
                            )
                          : ListView.builder(
                              itemCount: snapshot.data.length,
                              itemBuilder: (context, pos) {
                                var item = snapshot.data[pos];
                                return Container(
                                  child: Row(
                                    children: [
                                      10.hGapDp,
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            10.vGapDp,
                                            Text(
                                              item.medicineName,
                                              style: TextStyle(
                                                  fontSize: 18,
                                                  color: Colors.black,
                                                  fontWeight: FontWeight.bold),
                                            ),
                                            10.vGapDp,
                                            Row(
                                              children: [
                                                Text(
                                                  item.productFactory,
                                                  style: TextStyle(
                                                      fontSize: 12,
                                                      color: Colors.grey),
                                                ),
                                                Spacer(),
                                                Expanded(
                                                  flex: 8,
                                                  child: Text(
                                                    item.productSpec,
                                                    style: TextStyle(
                                                        fontSize: 12,
                                                        color: Colors.grey),
                                                  ),
                                                ),
                                                Spacer(),
                                                Text(
                                                  '¥${PrescribeUtil.formatMoney(item.unitPrice)}',
                                                  style: TextStyle(
                                                      fontSize: 12,
                                                      color: Colors.red),
                                                ),
                                              ],
                                            ),
                                            10.vGapDp,
                                            1.gapV(Colors.grey[300]),
                                          ],
                                        ),
                                      ),
                                      IconButton(
                                        onPressed: () {
                                          bloc.go2EditPage(context, item);
                                        },
                                        icon: Icon(
                                          Icons.add_circle_outline,
                                          color: theme.themeColor.themeColor,
                                        ),
                                      )
                                    ],
                                  ),
                                );
                              })
                      : buildLoadingIndicator();
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  MedicalSearchBloc generateBloc() {
    return MedicalSearchBloc();
  }
}
